
###############################################################################
# the macros in charge of dispatching to the language specific macros

macro(WRAP_LIBRARIES_ALL_LANGUAGES)
#!  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
#!    INIT_WRAP_LIBRARY_JAVA()
#!  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)

  if(WRAP_ITK_PYTHON)
    WRAP_LIBRARIES_PYTHON()
  endif(WRAP_ITK_PYTHON)
  if(WRAP_ITK_TCL)
    WRAP_LIBRARIES_TCL()
  endif(WRAP_ITK_TCL)
  if(WRAP_ITK_JAVA)
    WRAP_LIBRARIES_JAVA()
  endif(WRAP_ITK_JAVA)
  if(WRAP_ITK_RUBY)
    WRAP_LIBRARIES_RUBY()
  endif(WRAP_ITK_RUBY)
endmacro(WRAP_LIBRARIES_ALL_LANGUAGES)

macro(END_WRAP_LIBRARIES_ALL_LANGUAGES)
  if(WRAP_ITK_PYTHON)
    END_WRAP_LIBRARIES_PYTHON()
  endif(WRAP_ITK_PYTHON)
  if(WRAP_ITK_TCL)
    END_WRAP_LIBRARIES_TCL()
  endif(WRAP_ITK_TCL)
  if(WRAP_ITK_JAVA)
    END_WRAP_LIBRARIES_JAVA()
  endif(WRAP_ITK_JAVA)
  if(WRAP_ITK_RUBY)
    END_WRAP_LIBRARIES_RUBY()
  endif(WRAP_ITK_RUBY)
endmacro(END_WRAP_LIBRARIES_ALL_LANGUAGES)


macro(WRAP_LIBRARY_ALL_LANGUAGES library_name)
  if(WRAP_ITK_EXPLICIT)
    WRAP_LIBRARY_EXPLICIT("${library_name}")
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    WRAP_LIBRARY_GCCXML("${library_name}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    WRAP_LIBRARY_SWIG_INTERFACE("${library_name}")
  endif(WRAP_ITK_SWIGINTERFACE)
  if(WRAP_ITK_DOC)
    WRAP_LIBRARY_DOC("${library_name}")
  endif(WRAP_ITK_DOC)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    WRAP_LIBRARY_PYTHON("${library_name}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    WRAP_LIBRARY_TCL("${library_name}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    WRAP_LIBRARY_RUBY("${library_name}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    WRAP_LIBRARY_JAVA("${library_name}")
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
endmacro(WRAP_LIBRARY_ALL_LANGUAGES)

macro(END_WRAP_LIBRARY_ALL_LANGUAGES)
  if(WRAP_ITK_EXPLICIT)
    END_WRAP_LIBRARY_EXPLICIT()
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    END_WRAP_LIBRARY_GCCXML()
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    END_WRAP_LIBRARY_SWIG_INTERFACE()
  endif(WRAP_ITK_SWIGINTERFACE)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    END_WRAP_LIBRARY_PYTHON()
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    END_WRAP_LIBRARY_TCL()
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    END_WRAP_LIBRARY_RUBY()
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    END_WRAP_LIBRARY_JAVA()
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
  if(WRAP_ITK_DOC)
    END_WRAP_LIBRARY_DOC()
  endif(WRAP_ITK_DOC)
endmacro(END_WRAP_LIBRARY_ALL_LANGUAGES)


macro(WRAP_MODULE_ALL_LANGUAGES module)
  if(WRAP_ITK_EXPLICIT)
    WRAP_MODULE_EXPLICIT("${module}")
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    WRAP_MODULE_GCCXML("${module}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    WRAP_MODULE_SWIG_INTERFACE("${module}")
  endif(WRAP_ITK_SWIGINTERFACE)
  if(WRAP_ITK_DOC)
    WRAP_MODULE_DOC("${module}")
  endif(WRAP_ITK_DOC)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    WRAP_MODULE_PYTHON("${module}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    WRAP_MODULE_TCL("${module}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    WRAP_MODULE_RUBY("${module}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    WRAP_MODULE_JAVA("${module}")
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
endmacro(WRAP_MODULE_ALL_LANGUAGES)

macro(END_WRAP_MODULE_ALL_LANGUAGES module)
  if(WRAP_ITK_EXPLICIT)
    END_WRAP_MODULE_EXPLICIT("${module}")
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    END_WRAP_MODULE_GCCXML("${module}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    END_WRAP_MODULE_SWIG_INTERFACE("${module}")
  endif(WRAP_ITK_SWIGINTERFACE)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    END_WRAP_MODULE_PYTHON("${module}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    END_WRAP_MODULE_TCL("${module}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    END_WRAP_MODULE_RUBY("${module}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    END_WRAP_MODULE_JAVA("${module}")
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
  if(WRAP_ITK_DOC)
    END_WRAP_MODULE_DOC()
  endif(WRAP_ITK_DOC)
endmacro(END_WRAP_MODULE_ALL_LANGUAGES)


macro(WRAP_CLASS_ALL_LANGUAGES class)
endmacro(WRAP_CLASS_ALL_LANGUAGES)

macro(WRAP_NAMED_CLASS_ALL_LANGUAGES class swig_name)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    WRAP_NAMED_CLASS_PYTHON("${class}" "${swig_name}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    WRAP_NAMED_CLASS_TCL("${class}" "${swig_name}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    WRAP_NAMED_CLASS_RUBY("${class}" "${swig_name}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_DOC)
    WRAP_NAMED_CLASS_DOC("${class}" "${swig_name}")
  endif(WRAP_ITK_DOC)
endmacro(WRAP_NAMED_CLASS_ALL_LANGUAGES class swig_name)

macro(END_WRAP_CLASS_ALL_LANGUAGES)
endmacro(END_WRAP_CLASS_ALL_LANGUAGES)


macro(WRAP_NON_TEMPLATE_CLASS_ALL_LANGUAGES class)
endmacro(WRAP_NON_TEMPLATE_CLASS_ALL_LANGUAGES class)


macro(WRAP_NAMED_NON_TEMPLATE_CLASS_ALL_LANGUAGES class)
endmacro(WRAP_NAMED_NON_TEMPLATE_CLASS_ALL_LANGUAGES class)


macro(WRAP_INCLUDE_ALL_LANGUAGES inc)
  if(WRAP_ITK_EXPLICIT)
    WRAP_INCLUDE_EXPLICIT("${inc}")
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    WRAP_INCLUDE_GCCXML("${inc}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    WRAP_INCLUDE_SWIG_INTERFACE("${inc}")
  endif(WRAP_ITK_SWIGINTERFACE)
endmacro(WRAP_INCLUDE_ALL_LANGUAGES inc)

macro(WRAP_TEMPLATE_ALL_LANGUAGES name types)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    WRAP_TEMPLATE_PYTHON("${name}" "${types}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    WRAP_TEMPLATE_TCL("${name}" "${types}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    WRAP_TEMPLATE_RUBY("${name}" "${types}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
endmacro(WRAP_TEMPLATE_ALL_LANGUAGES)


macro(ADD_ONE_TYPEDEF_ALL_LANGUAGES wrap_method wrap_class swig_name template_params)
  if(WRAP_ITK_EXPLICIT)
    ADD_ONE_TYPEDEF_EXPLICIT("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_EXPLICIT)
  if(WRAP_ITK_GCCXML)
    ADD_ONE_TYPEDEF_GCCXML("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_SWIGINTERFACE)
    ADD_ONE_TYPEDEF_SWIG_INTERFACE("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_SWIGINTERFACE)
  if(WRAP_ITK_DOC)
    ADD_ONE_TYPEDEF_DOC("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_DOC)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    ADD_ONE_TYPEDEF_PYTHON("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    ADD_ONE_TYPEDEF_TCL("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    ADD_ONE_TYPEDEF_RUBY("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    ADD_ONE_TYPEDEF_JAVA("${wrap_method}" "${wrap_class}" "${swig_name}" "${template_params}")
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
endmacro(ADD_ONE_TYPEDEF_ALL_LANGUAGES)


macro(ADD_SIMPLE_TYPEDEF_ALL_LANGUAGES wrap_class swig_name)
  if(WRAP_ITK_GCCXML)
    ADD_SIMPLE_TYPEDEF_GCCXML("${wrap_class}" "${swig_name}")
  endif(WRAP_ITK_GCCXML)
  if(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
    ADD_SIMPLE_TYPEDEF_PYTHON("${wrap_class}" "${swig_name}")
  endif(WRAP_ITK_PYTHON AND WRAPPER_LIBRARY_PYTHON)
  if(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
    ADD_SIMPLE_TYPEDEF_TCL("${wrap_class}" "${swig_name}")
  endif(WRAP_ITK_TCL AND WRAPPER_LIBRARY_TCL)
  if(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
    ADD_SIMPLE_TYPEDEF_RUBY("${wrap_class}" "${swig_name}")
  endif(WRAP_ITK_RUBY AND WRAPPER_LIBRARY_RUBY)
  if(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
    ADD_SIMPLE_TYPEDEF_JAVA("${wrap_class}" "${swig_name}")
  endif(WRAP_ITK_JAVA AND WRAPPER_LIBRARY_JAVA)
endmacro(ADD_SIMPLE_TYPEDEF_ALL_LANGUAGES wrap_class swig_name)



###############################################################################
# ccache management. This option is shared by most of languages, so put it here.
option(WRAP_ITK_USE_CCACHE "Use ccache to cache swig/gccxml/... output and speedup the rebuild." OFF)
mark_as_advanced(WRAP_ITK_USE_CCACHE)
if(WRAP_ITK_USE_CCACHE)
  find_program(CCACHE_EXECUTABLE NAMES ccache-swig ccache DOC "ccache executable.")
endif(WRAP_ITK_USE_CCACHE)



###############################################################################
# build the languages list

if(EXTERNAL_WRAP_ITK_PROJECT)
  # languages there have been turned on while building wrapitk
  set(default ON)
else(EXTERNAL_WRAP_ITK_PROJECT)
  # set some of them to on by default
  option(WRAP_ITK_GCCXML "Build gccxml files." ON)
  option(WRAP_ITK_SWIGINTERFACE "Build swig interfaces." ON)
  set(default OFF)
endif(EXTERNAL_WRAP_ITK_PROJECT)

set(WRAP_ITK_LANGUAGES CACHE INTERNAL "Internal languages list.")
set(WRAP_ITK_UNUSED_LANGUAGES CACHE INTERNAL "Internal unused languages list.")
file(GLOB local_files *)
# filter and reorder the list
set(languages )
foreach(f ${local_files})
  file(RELATIVE_PATH lang "${CMAKE_CURRENT_SOURCE_DIR}" "${f}")
  if(IS_DIRECTORY "${f}" AND NOT "${lang}" MATCHES "CVS" AND NOT "${lang}" MATCHES ".svn")
    if("${lang}" MATCHES "^GccXML$|^SwigInterface$|^Doc$")
      # put those languages before the others
      set(languages ${lang} ${languages})
    else("${lang}" MATCHES "^GccXML$|^SwigInterface$|^Doc$")
      set(languages ${languages} ${lang})
    endif("${lang}" MATCHES "^GccXML$|^SwigInterface$|^Doc$")
  endif(IS_DIRECTORY "${f}" AND NOT "${lang}" MATCHES "CVS" AND NOT "${lang}" MATCHES ".svn")
endforeach(f)

foreach(lang ${languages})
  string(TOUPPER "${lang}" lang_upper)

  # add the language to the options
  option(WRAP_ITK_${lang_upper} "Build swig ${lang} wrapper support." ${default})

  # add the language to the list, if the user have turn it on, and include
  # the subdirectory
  if(WRAP_ITK_${lang_upper})
    set(WRAP_ITK_LANGUAGES ${WRAP_ITK_LANGUAGES} "${lang}")
    add_subdirectory("${lang}")
    get_directory_property(inc DIRECTORY "${lang}" INCLUDE_DIRECTORIES)
    include_directories(${inc})
  else(WRAP_ITK_${lang_upper})
    set(WRAP_ITK_UNUSED_LANGUAGES ${WRAP_ITK_UNUSED_LANGUAGES} "${lang}")
  endif(WRAP_ITK_${lang_upper})
endforeach(lang)
set(WRAP_ITK_LANGUAGES ${WRAP_ITK_LANGUAGES} CACHE INTERNAL "Internal languages list.")
set(WRAP_ITK_UNUSED_LANGUAGES ${WRAP_ITK_UNUSED_LANGUAGES} CACHE INTERNAL "Internal unused languages list.")

# message("WRAP_ITK_LANGUAGES: ${WRAP_ITK_LANGUAGES}")

if(NOT EXTERNAL_WRAP_ITK_PROJECT)
  install(FILES CMakeLists.txt itk.i
    DESTINATION "${WRAP_ITK_INSTALL_PREFIX}/Configuration/Languages"
  )
endif(NOT EXTERNAL_WRAP_ITK_PROJECT)
